﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GalaSoft.MvvmLight.CommandWpf;
using SpreadsheetGear;
using SpreadsheetGear.Windows.Controls;

namespace WPFWorkbookDesigner.Model.Commands
{
    public class MenuChecker
    {
        public static bool IsNotEditing(WorkbookView view)
        {
            return !view?.IsEditing ?? false;
        }
    }

    public class FileMenu
    {
        public static RelayCommand OpenWorkbookCommand { get; set; } = new RelayCommand(OpenWorkbook);
        public static RelayCommand AddWorkbookCommand { get; set; } = new RelayCommand(AddNewWorkbook);
        public static RelayCommand SaveWorkbookCommand { get; set; }
        public static RelayCommand CloseWorkbookCommand { get; set; }
        public static RelayCommand SaveAsCommand { get; set; }
        public static RelayCommand SaveAllCommand { get; set; }
        public static RelayCommand PageSetupCommand { get; set; }
        public static RelayCommand PriviewCommand { get; set; }
        public static RelayCommand PrintCommand { get; set; }

        #region Method

        private static readonly WindowManager WinManager = WindowManager.GetInstance();


        private static void OpenWorkbook()
        {
            using (var dia = GetOpenFileDialog())
            {
                if (dia.ShowDialog() != DialogResult.OK)
                    return;
                var set = WinManager.BookSet;
                foreach (var path in dia.FileNames)
                {
                    var book = set.Dowork(s => s.Workbooks.Open(path));
                    WinManager.CreateWindow(book);
                }
            }
        }

        private static void AddNewWorkbook()
        {
            var book = WinManager.BookSet.Dowork(s => s.Workbooks.Add());
            WinManager.CreateWindow(book);
        }
        private static void Save(WorkbookView view)
        {
            var book = view.ActiveWorkbook;
            if (!book.IsModified)
                return;
            if (book.FullName == book.Name && book.Name.StartsWith("Book"))
                SaveAs(view);//ps 可能需要改进文件保存逻辑
            else
            {
                view.Dowork(x => book.Save());
            }
            using (var dia = GetSaveFileDialog(book.Name))
            {
                var result = dia.ShowDialog();
                switch (result)
                {
                    case DialogResult.OK:
                        book.WorkbookSet.Dowork(s =>
                        {
                            book.Save();
                        });
                        break;
                    default:
                        return;
                }
            }
        }
        private static void SaveAs(WorkbookView view)
        {
            view.Dowork(v =>
            {
                var book = v.ActiveWorkbook;
                var dia = GetSaveFileDialog(book.Name);
                if (dia.ShowDialog() == DialogResult.OK)
                {
                    book.SaveAs(dia.FileName, FileFormat.OpenXMLWorkbook);
                }
            });
        }


        private static OpenFileDialog GetOpenFileDialog()
        {
            OpenFileDialog dia = new OpenFileDialog
            {
                Multiselect = true,
                Filter = Properties.Resources.FileDialogFormatFilter,
                FilterIndex = 1,
                AddExtension = true,
                CheckFileExists = true,
                Title = "打开文件"
            };
            return dia;
        }

        private static SaveFileDialog GetSaveFileDialog(string fileName, string title = "保存")
        {
            var dia = new SaveFileDialog
            {
                FilterIndex = 2,
                OverwritePrompt = true,
                FileName = fileName,
                Title = title,
                CheckPathExists = true,
                ValidateNames = true,
                AutoUpgradeEnabled = true,
                Filter = Properties.Resources.FileDialogFormatFilter,
            };
            return dia;
        }
        #endregion

    }

    public class AddonsMenu
    {

    }
}
